package com.ali.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class TestAop {
    //切点
    @Pointcut("execution(* com.ali.service.impl.TestServiceImpl.hello(..))")
    public void PointCut() {
    }

    //环绕通知
    @Around("execution(* com.ali.service.impl.TestServiceImpl.hello(..))")
    public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("环绕通知");

        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();

        log.info("环绕通知 - 执行方法名：" + method.getName());

        //正常执行方法
        return proceedingJoinPoint.proceed();

        //取消执行方法
//        return null;
    }

    //前置通知
    @Before("execution(* com.ali.service.impl.TestServiceImpl.hello(..))")
    public void Before() {
        log.info("前置通知");
    }

    //返回通知
    @AfterReturning("execution(* com.ali.service.impl.TestServiceImpl.hello(..))")
    public void AfterReturning() {
        log.info("返回通知");
    }

    //异常通知
    @AfterThrowing("execution(* com.ali.service.impl.TestServiceImpl.hello(..))")
    public void AfterThrowing() {
        log.info("异常通知");
    }

    //后置通知
    @After("execution(* com.ali.service.impl.TestServiceImpl.hello(..))")
    public void After() {
        log.info("后置通知");
    }
}